﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Serialization;

namespace XEvents.Library
{
    /// <summary>
    /// EventSessionTargetOption Object
    /// </summary>
    [Serializable]
    [XmlType("TargetOption")]
    public struct EventSessionTargetOption
    {
        private string name;// = string.Empty;
        private string value;// = string.Empty;

        /// <summary>
        /// Determines whether the EventSessionTargetOption is equal to the current EventSessionTargetOption
        /// </summary>
        /// <param name="obj">EventSessionTargetOption to be compared to.</param>
        /// <returns>bool</returns>
        public override bool Equals(object obj)
        {
            EventSessionTargetOption to = (EventSessionTargetOption)obj;
            if (to.name != name)
                return false;
            if (to.value != value)
                return false;
            return true;

        }

        /// <summary>
        /// Returns the hash code for this EventSessionTargetOption
        /// </summary>
        public override int GetHashCode()
        {
            return name.GetHashCode() ^ value.GetHashCode();
        }

        /// <summary>
        /// EventSessionTargetOption Name
        /// </summary>
        [XmlAttribute("Name")]
        public string Name
        {
            get { return name; }
            set
            {
                if (value == null)
                    name = string.Empty;
                else
                    name = value;
            }
        }

        /// <summary>
        /// EventSessionTargetOption Value
        /// </summary>
        [XmlAttribute("Value")]
        public string Value
        {
            get { return value; }
            set
            {
                if (this.value == null)
                    this.value = string.Empty;
                else
                    this.value = value;
            }
        }

        /// <summary>
        /// Returns a new EventSessionTargetOption
        /// </summary>
        /// <param name="name">EventSessionTargetOption Name</param>
        /// <param name="value">EventSessionTargetOption Value</param>
        public EventSessionTargetOption(string Name, string Value)
        {
            if (Value == null)
                value = string.Empty;
            else
                value = Value;

            if (Name == null)
                name = string.Empty;
            else
                name = Name;
        }


        internal EventSessionTargetOption(server_event_session_field sesf)
        {
            this.name = sesf.name;
            this.value = sesf.value;
        }

        /// <summary>
        /// Script this EventSessionTargetOption
        /// </summary>
        public string ScriptOption()
        {
            int val;
            if (Int32.TryParse(value, out val))
            {
                return string.Format("{0}={1}", name, value);
            }
            else
            {
                return string.Format("{0}='{1}'", name, value);
            }
        }

    }
}
